<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Fixtures</title>
<link rel="stylesheet" href="../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Boost.Test">
<link rel="up" href="../tests_organization.html" title="Declaring and organizing tests">
<link rel="prev" href="decorators/explicit_decorator_declaration.html" title="Explicit decorator declaration">
<link rel="next" href="fixtures/models.html" title="Fixture models">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="decorators/explicit_decorator_declaration.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tests_organization.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="fixtures/models.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_test.tests_organization.fixtures"></a><a class="link" href="fixtures.html" title="Fixtures">Fixtures</a>
</h3></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="fixtures/models.html">Fixture
        models</a></span></dt>
<dt><span class="section"><a href="fixtures/case.html">Test case
        fixture</a></span></dt>
<dt><span class="section"><a href="fixtures/per_test_suite_fixture.html">Test
        suite entry/exit fixture</a></span></dt>
<dt><span class="section"><a href="fixtures/global.html">Global
        fixture</a></span></dt>
</dl></div>
<p>
        In general terms a test fixture or test context is the collection of one
        or more of the following items, required to perform the test:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            preconditions
          </li>
<li class="listitem">
            particular states of tested units
          </li>
<li class="listitem">
            necessary cleanup procedures
          </li>
</ul></div>
<p>
        Though these tasks are encountered in many if not all test cases, what makes
        a test fixture different is repetition. Where a normal test case implementation
        does all preparatory and cleanup work itself, a test fixture allows it to
        be implemented in a separate reusable unit.
      </p>
<p>
        With introduction of e<span class="bold"><strong>X</strong></span>treme <span class="bold"><strong>P</strong></span>rogramming
        (XP), the testing style, that require test setup/cleanup repetition, has
        become even more popular. Single XP adopted test modules may contain hundreds
        of single assertion test cases, many requiring very similar test setup/cleanup.
        This is the problem that the test fixture is designed to solve.
      </p>
<p>
        In practice a test fixture usually is a combination of <code class="computeroutput"><span class="identifier">setup</span></code>
        and <code class="computeroutput"><span class="identifier">teardown</span></code> functions, associated
        with test case. The former serves the purposes of test setup. The later is
        dedicated to the cleanup tasks. Ideally we'd like for a test module author
        to be able to define variables used in fixtures on the stack and, at the
        same time, to refer to them directly in a test case.
      </p>
<p>
        It's important to understand that C++ provides a way to implement a straightforward
        test fixture solution that almost satisfies our requirements without any
        extra support from the test framework. Here is how simple test module with
        such a fixture may look like:
      </p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">MyFixture</span> <span class="special">{</span>
  <span class="identifier">MyFixture</span><span class="special">()</span>   <span class="special">{</span> <span class="identifier">i</span> <span class="special">=</span> <span class="keyword">new</span> <span class="keyword">int</span><span class="special">;</span> <span class="special">*</span><span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span> <span class="special">}</span>
  <span class="special">~</span><span class="identifier">MyFixture</span><span class="special">()</span>  <span class="special">{</span> <span class="keyword">delete</span> <span class="identifier">i</span><span class="special">;</span> <span class="special">}</span>

  <span class="keyword">int</span><span class="special">*</span> <span class="identifier">i</span><span class="special">;</span>
<span class="special">};</span>

<a class="link" href="../utf_reference/test_org_reference/test_org_boost_auto_test_case.html" title="BOOST_AUTO_TEST_CASE"><code class="computeroutput"><span class="identifier">BOOST_AUTO_TEST_CASE</span></code></a><span class="special">(</span> <span class="identifier">test_case1</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">MyFixture</span> <span class="identifier">f</span><span class="special">;</span>
  <span class="comment">// do something involving f.i</span>
<span class="special">}</span>

<a class="link" href="../utf_reference/test_org_reference/test_org_boost_auto_test_case.html" title="BOOST_AUTO_TEST_CASE"><code class="computeroutput"><span class="identifier">BOOST_AUTO_TEST_CASE</span></code></a><span class="special">(</span> <span class="identifier">test_case2</span> <span class="special">)</span>
<span class="special">{</span>
  <span class="identifier">MyFixture</span> <span class="identifier">f</span><span class="special">;</span>
  <span class="comment">// do something involving f.i</span>
<span class="special">}</span>
</pre>
<p>
        This is a generic solution that can be used to implement any kind of shared
        setup or cleanup procedure. Still there are several more or less minor practical
        issues with this pure C++ based fixtures solution:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            We need to add a fixture declaration statement into each test case manually.
          </li>
<li class="listitem">
            Objects defined in fixture are references with <code class="computeroutput"><span class="special">&lt;</span><span class="identifier">fixture</span><span class="special">-</span><span class="identifier">instance</span><span class="special">-</span><span class="identifier">name</span><span class="special">&gt;</span></code>
            prefix.
          </li>
<li class="listitem">
            There is no place to execute a <span class="emphasis"><em>global</em></span> fixture, which
            performs <span class="emphasis"><em>global</em></span> setup/cleanup procedures before
            and after testing.
          </li>
</ul></div>
<p>
        The <span class="emphasis"><em>Unit Test Framework</em></span> lets you define a fixture according
        to <a class="link" href="fixtures/models.html" title="Fixture models">several
        generic interfaces</a>, and thus helps you with following tasks:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            define shared setup/teardown procedures for a single or group of test
            cases
          </li>
<li class="listitem">
            define setup/teardown procedures which are performed once per test suite
          </li>
<li class="listitem">
            define <a class="link" href="fixtures/global.html" title="Global fixture">global
            setup/teardown</a> procedures which are performed once per test module
          </li>
</ul></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2022 Boost.Test contributors<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="decorators/explicit_decorator_declaration.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tests_organization.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="fixtures/models.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
